﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ProduceBasicData.Api.Application.Command.RBAC.Department;
using ProduceBasicData.Api.Application.Command.RBAC.Permission;
using ProduceBasicData.Domain.RBAC;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using ProduceBasicData.Api.Services;

namespace ProduceBasicData.Api.Application.Handler.RBAC.Permission
{
    /// <summary>
    /// 权限录入处理类
    /// </summary>
    public class CreatePermissionHandler : IRequestHandler<CreatePermissionCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<PermissionModel> _permissionRepository;
        private readonly IMapper _mapper;
        private readonly ICodeRuleService codeRuleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="permissionRepository"></param>
        /// <param name="mapper"></param>
        public CreatePermissionHandler(IBaseRespository<PermissionModel> permissionRepository, IMapper mapper,
            ICodeRuleService codeRuleService)
        {
            _permissionRepository = permissionRepository;
            _mapper = mapper;
            this.codeRuleService = codeRuleService;
        }
        /// <summary>
        /// 处理权限录入请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(CreatePermissionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //将命令转换为领域模型
                var permission = _mapper.Map<PermissionModel>(request);
                //判断权限是否存在
                var permissionExist = await _permissionRepository.GetAll().AnyAsync(x => x.PermissionName == permission.PermissionName);
                if (permissionExist != false)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "权限已存在"
                    };
                }

                //编码
                if (string.IsNullOrEmpty(permission.PermissionCode))
                {
                    var code = await codeRuleService.AutoGenerateCodeAsync("ProduceBasicData.Domain.RBAC.PermissionModel");
                    if (!string.IsNullOrEmpty(code))
                    {
                        permission.PermissionCode = code;
                    }
                    else
                    {
                        permission.PermissionCode = $"QX{DateTime.Now:yyyyMMddHHmmss}";
                    }
                }

                //保存类型
                var result = await _permissionRepository.AddAsync(permission);
                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Data = result,
                        Code = ApiEnum.Success,
                        Msg = "权限录入成功"
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "信息录入失败"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Failed,
                    Msg = ex.Message
                };
            }
        }
    }
}
